
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SetPlayer(ply)
	self:SetOwner(ply)
end

function ENT:MakeActive()
	self.IsActive = true
	self.AutoKillTime = CurTime() + 10
	
	local pos = self:GetPos()
	local ep = Vector(0,0,-500)
	local add = Vector(0,0,-35)
	local tracedata = {}
	tracedata.start = pos+add
	tracedata.endpos = pos+ep
	local trace = util.TraceLine(tracedata) 
	
	if trace.Hit then self.NoAirSound = true end
	
end

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:SetModel( "models/props_junk/propane_tank001a.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	self:SetColor(225,200,200,255)
	self.EmitOnce = false
	self.HitCounter = math.random(2,5)
	self.Die = false
	self.IsActive = false
	self.NoAirSound = false
	self.TimedDie = nil
	self.AutoKillTime = nil
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMass(100)
	end
	
end

function ENT:BombExplode()

	self:EmitSound(ChooseRandom(GameSounds.BombExplosion),math.random(100,130),math.random(75,135))
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos())
	util.Effect("dropbomb_explosion", effectdata)
	
	self:ExplosionDamage(self:GetOwner(),self:GetPos(),250,370)// 250 dmg
	self:FireMaker(self:GetOwner(),self:GetPos(),1,3,90)  //80 radius 
	
	self:Fire("kill",1,0.001)

end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()
	
	if self.DelaySound then
		if self.DelaySound < CurTime() then
			self:EmitSound(self.PSound,100,self.DPitch)
			self.DelaySound = nil
		end
	end
	if self.DelayParent then
		if self.DelayParent < CurTime() then
			self:SetParent(nil)
			self.DelayParent = nil
			self:MakeActive()
			local phys = self:GetPhysicsObject()
			if phys:IsValid() then
			phys:ApplyForceCenter(self:GetOwner():GetVehicle():GetVelocity())
			end
		end
	end

	if self.AutoKillTime != nil then
		if self.AutoKillTime < CurTime() then BombExplode(self) end
	end

	if self.IsActive == false then return end
	if self.Die then self:BombExplode() end

	if self.EmitOnce == true then return end

	local pos = self:GetPos()
	local ep = Vector(0,0,-1400)
	local add = Vector(0,0,-35)
	local tracedata = {}
	tracedata.start = pos+add
	tracedata.endpos = pos+ep
	local trace = util.TraceLine(tracedata) 
	
	if trace.Hit and self.EmitOnce == false and self.NoAirSound == false then
		util.PrecacheSound("weapons/mortar/mortar_shell_incomming1.wav")
		self:EmitSound("weapons/mortar/mortar_shell_incomming1.wav",90,math.random(65,85))
		self.EmitOnce = true
	end
	
	if self.NoAirSound != false and self.TimedDie == nil then
		self.TimedDie = CurTime() + 5
	end
	
	if self.TimedDie != nil then
		if self.TimedDie < CurTime() then
			self.Die = true
		end
	end
	
	if self:WaterLevel() > 0 then
		self.Die = true
	end
  
 end 
 
 
 /*---------------------------------------------------------
   Name: takedamage
---------------------------------------------------------*/
 function ENT:OnTakeDamage( dmginfo )
	
	self.HitCounter = self.HitCounter - 1
	
	if self.HitCounter <= 0 then
		self.Die = true
	end
	
	// React physically when shot/getting blown
	self:TakePhysicsDamage( dmginfo )
	
end


/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )

	if (data.Speed > 780 && data.DeltaTime > 0.2 ) then
		self:BombExplode()
	elseif (data.Speed > 100 && data.DeltaTime > 0.2 ) then
		local s = "physics/metal/metal_canister_impact_hard"..math.random(1,3)..".wav"
		util.PrecacheSound(s)
		self:EmitSound(s,math.random(60,100),math.random(80,120))
	end
	
end